package org.dawn.deveco.plugin.resourceeditor.model;

import org.dawn.deveco.plugin.resourceeditor.common.Constants;
import org.dawn.deveco.plugin.resourceeditor.model.item.BaseItem;
import org.dawn.deveco.plugin.resourceeditor.model.item.MediaItem;
import org.dawn.deveco.plugin.resourceeditor.utils.Logger;

import javax.swing.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import java.util.stream.Collectors;

public class MediaResourceContent extends BaseResourceContent {
    public static final int ICON_COLUMN_INDEX = 3;
    public static final int FULL_FILENAME_COLUMN_INDEX = 2;

    private static final Logger LOG = Logger.getInstance(MediaResourceContent.class);

    public MediaResourceContent() {
        setCategory(Constants.MEDIA_CATEGORY);
        setColumns(new ColumnConfig("Name", String.class, true, true),
                new ColumnConfig("Filename", String.class, true, false, false, "图片文件名"),
                new ColumnConfig("FullFilename", String.class, false, false, false,"图片文件全路径"),
                new ColumnConfig("", Icon.class, true, false, false, "图片缩略图"),
                new ColumnConfig("", Icon.class, true, false, false, "删除按钮"));
       }

    /**
     * 返回 DefaultTableModel 的数据
     * @param group 在 Media 类型数据中，由于只有一个media文件夹，group 被忽略
     * @return
     */
    public Vector<Vector> getJTableContent(String group) {
        List<? extends BaseItem> itemList = this.getGroupData(group);
        Vector<Vector> list = new Vector<>();
        for (final BaseItem e : itemList){
            MediaItem item = (MediaItem) e;
            Vector<Object> vec = new Vector<>(3);
            vec.add(item.name);
            vec.add(item.fileName);
            vec.add(item.fullFileName);
            vec.add(null);   // Icon 列
            vec.add(null);   // Action 列
            list.add(vec);
        }
        return list;
    }

    public boolean save() {
        boolean ret = false;

        String path = Constants.RESOURCE_PATH_MAP.get(Constants.MEDIA_CATEGORY);
        Path mediaDir = Paths.get(Constants.RESOURCE_ROOT_PATH, path);

        Vector<Vector> data = this.tableDataByGroup.get(MediaResourceContent.DEFAULT_GROUP);

        try {
            List<Path> existedFileList = Files.list(mediaDir).filter(Files::isRegularFile)
                    .collect(Collectors.toList());
            List<Path> mediaList = data.stream().filter(item -> !item.isEmpty())
                    .map(item -> Paths.get((String) item.get(MediaResourceContent.FULL_FILENAME_COLUMN_INDEX)))
                    .collect(Collectors.toList());
            List<Path> newList = new ArrayList<>(mediaList);
            // 复制新增的 media 文件到 media 目录
            newList.removeAll(existedFileList);
//            LOG.debug("newList: " + newList);
            for (Path mediaPath : newList) {
                Path newFilePath = Paths.get(mediaDir.toString(), mediaPath.getFileName().toString());
                Files.deleteIfExists(newFilePath);
                Files.copy(mediaPath, newFilePath);
            }
            // 删除已删除的 media 文件
            existedFileList.removeAll(mediaList);
//            LOG.debug("removedList: " + existedFileList);
            for (Path mediaPath : existedFileList) {
                Files.deleteIfExists(mediaPath);
            }
            ret = true;
        } catch (Exception e) {
            LOG.error("failed to save resource file to dir: " + mediaDir, e);
        }

        if (ret) {
            setChanged(false);
        }
        return ret;
    }

}
